﻿module BoardTool.KillFluff

open System
open System.Collections.Generic

open BoardTool.Tool
open BoardTool.Commands
open BoardTool.Board

let private killFluffInFile srcFile destFile =
    let defluffed = ref false
    
    let killFluff hex =
        let hex' = 
            { hex with
                Theme = ""
                Terrains = hex.Terrains |> List.filter (fun t -> not( t.Type = "fluff" ))
            }
        if hex' <> hex then defluffed := true
        hex'

    srcFile
    |> parseBoardFile
    |> Array.map (mapHex killFluff)
    |> unparseBoardFile destFile

    !defluffed

type private KillFluffCommand( srcDir, destDir ) =
    inherit TransformDirCommand<bool>( srcDir, destDir )

    override this.transformFile srcFile destFile =
        killFluffInFile srcFile destFile
        
    override this.reduce results =
        printfn ""
        let filesDefluffed = 
            seq { for k in results.Keys do yield k, results.[k] }
            |> Seq.filter snd
            |> Seq.map fst
            |> Seq.toArray
        printfn "Files defluffed: %d" filesDefluffed.Length
        filesDefluffed |> Array.iter (printfn "  %s")
        
    override this.Id = 
        sprintf "(killfluff %s %s)" srcDir destDir

let KILL_FLUFF_COMMAND_FACTORY =
    { new TransformDirCommandFactory( "killfluff", "removes fluff hexes and themes" ) with
        override this.createCommand( srcDir, destDir ) =
            KillFluffCommand( srcDir, destDir ) :> ICommand
    } :> ICommandFactory